﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Deployment.Application;
using ESF.General;
using System.Net;
using System.Threading;
using System.ComponentModel;
using System.ServiceModel;
using ESF.SFT.FileUploader.Presenter.General;
using ESF.WinForms.Elements;
using ESF.SFT.FileUploader.SharedResources;
using ESF.WinForms;
using ESF.SFT.Proxies;
using ESF.General.Entity;

#endregion


namespace ESF.SFT.FileUploader
{
	/// <summary>
	/// Interaction logic for SplashScreen.xaml
	/// </summary>
	public partial class SplashScreen : BaseWPFWindow, ISplashScreenViewer
	{
		#region Miembros

		private BackgroundWorker _Bg;

		private SplashScreenPresenter _Presenter;

		#endregion


		#region Constructor
		
		public SplashScreen() : base ()
		{
			InitializeComponent();

			imgLogo.Source = WinFormsHelper.ToBitmapSource(SharedImages.Upload_64x64);

			Icon = WinFormsHelper.ToBitmapSource(SharedImages.Upload_64x64);

			_Presenter = new SplashScreenPresenter (this);
			
			Show ();

			IncludeVersionInTitle();

			_Bg = new BackgroundWorker();

			_Bg.DoWork += delegate(object s, DoWorkEventArgs eg) { _Presenter.Load (); };
			_Bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_Bg_RunWorkerCompleted);
			_Bg.RunWorkerAsync();
		}

		#endregion


		#region Methods
		
		public void CaptureProxySettings ()
		{
			Dispatcher.Invoke((Action)(() =>
			{
				Program.winICO = new InternetConnectionOptions();

				Program.winICO.Owner = this;
				Program.winICO.ShowDialog();
			}));
		}

		#endregion


		#region Background Worker Events

		protected void _Bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if (e.Error != null)
			{
				LogError(e.Error);

				CloseApplication ();

				return;
			}

			Log.StopOps();
			
			// Para permitir ver el mensaje de completado
			Thread.Sleep (500);

			Dispatcher.Invoke ((Action) delegate 
			{
				Hide();

				Program.winSignIn = new SignInWindow(); 
				Program.winSignIn.Show();
			});
		}

		#endregion


		#region ISplashScreenViewer Fields

		public int LoadingProgress
		{
			set { Dispatcher.Invoke((Action)(() => { prgProgressBar.Value = value; })); }
		}

		public string ProductName
		{
			set { Dispatcher.Invoke((Action)(() => { lblProductName.Content = value; })); }
		}

		public string ProductVersion
		{
			set { Dispatcher.Invoke((Action)(() => { lblVersion.Content = value; })); }
		}

		public string PublisherName
		{
			set { Dispatcher.Invoke((Action)(() => { lblPublisherName.Content = value; })); }
		}

		public string CurrentStep
		{
			set { Log.CurrentStep = value; Dispatcher.Invoke((Action)(() => { lblLoadingMessage.Content = value + "... "; })); }
		}

		public List<Couple<string, string>> Conf
		{
			set { Program.Conf = value; }
		}

		#endregion
	}
}
